import React, { FC, ReactText, useMemo } from 'react';
import { getMessage, TextWidget } from '@inbiz/react';
import { Cascader, Empty } from 'antd';
import { SingleCascaderProps } from 'rc-cascader';
import { IOptionType, TargetLevelProps } from '../../type';
import './index.less';
import { GlobalRegistry } from '@inbiz/core';
import { observer } from '@formily/react';
interface IProps extends Partial<Omit<SingleCascaderProps, 'options'>> {
  labelMap: { [p: string]: string[] };
  className?: string;
  dropdownClassName?: string;
  options: IOptionType[];
  placeholder?: string;
  value?: ReactText[];
  targetLevel?: TargetLevelProps;
  onChange?: (v: ReactText[], vOptions: IOptionType[]) => void;
}
const SingleView: FC<IProps> = ({
  className,
  dropdownClassName,
  options,
  placeholder,
  value,
  targetLevel,
  labelMap,
  onChange,
  ...restProps
}) => {

const notFoundContent = useMemo(() => {
  return getMessage('Cascader.notFoundContent')
}, [GlobalRegistry.getDesignerLanguage()])
  return (
    <Cascader
      {...restProps}
      className={className}
      dropdownClassName={`${dropdownClassName} ${options.length < 1 ? 'empty' : ''}`}
      changeOnSelect={targetLevel !== 'leaf'}
      options={options}
      placeholder={placeholder}
      value={value}
      style={{display:'block'}}
      onChange={onChange}
      displayRender={(label: ReactText[]) => {
        if (label.length) {
          const v = labelMap[[...label].pop() || ''];
          if (v) {
            return v.join('/');
          } else {
            return label.join('/');
          }
        } else {
          return label.join('/');
        }
      }}
      notFoundContent={notFoundContent
      }
    />
  );
};

export default observer(SingleView);
